#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc.h"



/********************************************************************************************
********************************************************************************************
    CH13726  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    CH13726  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    CH13726  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    CH13726  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    CH13726  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
********************************************************************************************

********************************************************************************************/
int GrayIndex[34]={255,254,252,250,248,244,236,228,220,212,204,196,188,180,164,148,132,124,116,100,84,68,52,44,36,28,20,16,12,8,4,3,1,0};
u8 offIndex[34]={0x0A,0x0A,0x05,0x05,0x05,0x05,0x00,0x00,0x00,0x00,0x0A,0x0A,0x0A,0x0A,0x05,0x05,0x05,0x05,0x00,0x00,0x00,0x00,0x0A,0x0A,0x0A,0x0A,0x05,0x05,0x05,0x05,0x00,0x00,0x00,0x00};
u8 subIndex[34]={0x0C,0x0B,0x09,0x08,0x07,0x06,0x04,0x03,0x02,0x01,0x0E,0x0D,0x0C,0x0B,0x09,0x08,0x07,0x06,0x04,0x03,0x02,0x01,0x0E,0x0D,0x0C,0x0B,0x09,0x08,0x07,0x06,0x04,0x03,0x02,0x01};
u8 offset[34]={2,0,6,4,2,0,6,4,2,0,6,4,2,0,6,4,2,0,6,4,2,0,6,4,2,0,6,4,2,0,6,4,2,0};
u8 reg[34]={0xb4,0xb4,0xb4,0xb4,0xb4,0xb4,0xb4,0xb4,0xb4,0xb4,0xb3,0xb3,0xb3,0xb3,0xb3,0xb3,0xb3,0xb3,0xb3,0xb3,0xb3,0xb3,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2,0xb2};
void 	CH13726_AutoGamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
	 u8 buffer1[7];
	 	SSD2828_W_Reg(SigMode,channel,0xB7,0x0019);
	//gamma调节设置
//		buffer1[0] = 0x02;        
//    buffer1[1] = 0xf0;   
//		buffer1[2] = 0x58;  
//    SSD2828_W_Array(SigMode,channel,buffer1,0);
//	
//		buffer1[0] = 0x04;        
//    buffer1[1] = 0xc0;   
//		buffer1[2] = 0x03;  
//	  buffer1[3] = 0x6b; 
//  	buffer1[4] = 0x94; 
//    SSD2828_W_Array(SigMode,channel,buffer1,0);
//		delay_ms(5);

	
	  //SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		buffer1[0] = 0x02;        
    buffer1[1] = 0xf0;   
		buffer1[2] = 0x52;  
    SSD2828_W_Array(SigMode,channel,buffer1,0);
	 //r
		buffer1[0] = 0x02;        
    buffer1[1] = 0xf1;   
		buffer1[2] = offIndex[ buffer[3]];  
    SSD2828_W_Array(SigMode,channel,buffer1,0);
		buffer1[0] = 0x02;        
    buffer1[1] = reg[buffer[3]];   
		buffer1[2] = buffer[4];  
    SSD2828_W_Array(SigMode,channel,buffer1,0);
	
	
		buffer1[0] = 0x02;        
    buffer1[1] = 0xf1;   
		buffer1[2] = subIndex[ buffer[3]];  
    SSD2828_W_Array(SigMode,channel,buffer1,0);
		buffer1[0] = 0x02;        
    buffer1[1] = reg[buffer[3]];   
		buffer1[2] = buffer[5];  
    SSD2828_W_Array(SigMode,channel,buffer1,0);
		//g
		buffer1[0] = 0x02;        
    buffer1[1] = 0xf1;   
		buffer1[2] = offIndex[ buffer[3]];  
    SSD2828_W_Array(SigMode,channel,buffer1,0);
		buffer1[0] = 0x02;        
    buffer1[1] = reg[buffer[3]]+3;   
		buffer1[2] = buffer[6];  
    SSD2828_W_Array(SigMode,channel,buffer1,0);
	
		buffer1[0] = 0x02;        
    buffer1[1] = 0xf1;   
		buffer1[2] = subIndex[ buffer[3]];  
    SSD2828_W_Array(SigMode,channel,buffer1,0);
		buffer1[0] = 0x02;        
    buffer1[1] = reg[buffer[3]]+3;   
		buffer1[2] = buffer[7];  
    SSD2828_W_Array(SigMode,channel,buffer1,0);
		//b
		buffer1[0] = 0x02;        
    buffer1[1] = 0xf1;   
		buffer1[2] = offIndex[ buffer[3]];  
    SSD2828_W_Array(SigMode,channel,buffer1,0);
		buffer1[0] = 0x02;        
    buffer1[1] = reg[buffer[3]]+6;   
		buffer1[2] = buffer[8];  
    SSD2828_W_Array(SigMode,channel,buffer1,0);

		buffer1[0] = 0x02;        
    buffer1[1] = 0xf1;   
		buffer1[2] = subIndex[ buffer[3]];  
    SSD2828_W_Array(SigMode,channel,buffer1,0);
		buffer1[0] = 0x02;        
    buffer1[1] = reg[buffer[3]]+6;   
		buffer1[2] = buffer[9];  
    SSD2828_W_Array(SigMode,channel,buffer1,0);

}
	
	
void CH13726_Write_Register(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
		if(USB_Rx_Buffer[1]&0x80)
			SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0xFFFD);
		else
			SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
      SSD2828_W_Array(SigMode,channel,buffer,2);                               

    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写寄存器状态  ：ok  
}
void CH13726_Write_51Register(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    SSD2828_W_Reg(SigMode,channel,0xB7,0x0019);
	  //SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(5);
    SSD2828_W_Array(SigMode,channel,buffer,2);                               

    delay_ms(5);
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写寄存器状态  ：ok  
}
void CH13726_Read_Register(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
   u16 tmp;
    buffer[1]=0x03;
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);                               
    SSD2828_W_Reg(SigMode,channel,0xC1,buffer[2]); //return package size                                
    SSD2828_W_Reg(SigMode,channel,0xC0,0x0001);
    buffer[0] = 0x01;
    buffer[1] = buffer[3];                        
    SSD2828_W_Array(SigMode,channel,buffer,0);
    delay_ms(5);  
    SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
    SSD2828_W_Cmd(SigMode,channel,0xFF);
    for(i=0;i<buffer[2]-1;i++)
    {
        SSD2828_W_Cmd(SigMode,channel,0xFA);		
        tmp=SPI3_Read_u16_Data(channel);
        buffer[4+i]=tmp>>8;
        buffer[5+i]=tmp;
        delay_ms(5);
        i++;
    }
		buffer[0]=0x71;	
		buffer[1]=0x02;
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 寄存器数据  ：ok 
}

void CH13726_Write_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[7];
    u8 i,j,m,k=0,temp;
	//进入gamma调节模式
	  SSD2828_W_Reg(SigMode,channel,0xB7,0x0019);
		buffer1[0] = 0x02;        
    buffer1[1] = 0xf0;   
		buffer1[2] = 0x58;  
    SSD2828_W_Array(SigMode,channel,buffer1,0);
	
		buffer1[0] = 0x04;        
    buffer1[1] = 0xc0;   
		buffer1[2] = 0x03;  
	  buffer1[3] = 0x6b; 
  	buffer1[4] = 0x94; 
    SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);
	  buffer1[0] = 0x02;        
    buffer1[1] = 0xf0;   
		buffer1[2] = 0x52;  
    SSD2828_W_Array(SigMode,channel,buffer1,0);

	  if(buffer[3]==0x01)       //GAMMA RED
	  {
	     
				 SSD2828_W_Reg(SigMode,channel,0xBC,16); 
		     SSD2828_W_Cmd(SigMode,channel,0xBF);	
			   SSD2828_W_Data(SigMode,channel,0xB2); //GAMMA RED GROUP1
		   
				 for( i = 0;i<15;i++)   //15个绑点
				 {	                                                                                  
					 SSD2828_W_Data(SigMode,channel,buffer[i+5]);	
					 delay_ms(5);  
				 }
			 
				 SSD2828_W_Reg(SigMode,channel,0xBC,16); 
		     SSD2828_W_Cmd(SigMode,channel,0xBF);	
			   SSD2828_W_Data(SigMode,channel,0xB3); //GAMMA RED GROUP2
			 	 for( i = 0;i<15;i++)   //15个绑点
				 {	                                                                                  
					 SSD2828_W_Data(SigMode,channel,buffer[i+5+15]);	
					 delay_ms(5);  
				 }
			 
				 SSD2828_W_Reg(SigMode,channel,0xBC,14); 
		     SSD2828_W_Cmd(SigMode,channel,0xBF);	
			   SSD2828_W_Data(SigMode,channel,0xB4); //GAMMA RED GROUP3
				 for( i = 0;i<13;i++)   //15个绑点
				 {	                                                                                  
					 SSD2828_W_Data(SigMode,channel,buffer[i+5+30]);	
					 delay_ms(5);  
				 }
		  
	  }
		else if(buffer[3]==0x02)  //GAMMA GREEN
		{
			
				 SSD2828_W_Reg(SigMode,channel,0xBC,16); 
		     SSD2828_W_Cmd(SigMode,channel,0xBF);	
			   SSD2828_W_Data(SigMode,channel,0xB5); //GAMMA GREEN GROUP1
		     for( i = 0;i<15;i++)   //15个绑点
				 {	                                                                                  
					 SSD2828_W_Data(SigMode,channel,buffer[i+5]);	
					 delay_ms(5);  
				 }
				 SSD2828_W_Reg(SigMode,channel,0xBC,16); 
		     SSD2828_W_Cmd(SigMode,channel,0xBF);	
			   SSD2828_W_Data(SigMode,channel,0xB6); //GAMMA GREEN GROUP2
		     for( i = 0;i<15;i++)   //15个绑点
				 {	                                                                                  
					 SSD2828_W_Data(SigMode,channel,buffer[i+5+15]);	
					 delay_ms(5);  
				 }
				 SSD2828_W_Reg(SigMode,channel,0xBC,14); 
		     SSD2828_W_Cmd(SigMode,channel,0xBF);	
			   SSD2828_W_Data(SigMode,channel,0xB7); //GAMMA GREEN GROUP3
				 for( i = 0;i<15;i++)   //15个绑点
				 {	                                                                                  
					 SSD2828_W_Data(SigMode,channel,buffer[i+5+30]);	
					 delay_ms(5);  
				 }
		   
		}
		else if(buffer[3]==0x03)   //GAMMA BLUE
		{
		
				 SSD2828_W_Reg(SigMode,channel,0xBC,16); 
		     SSD2828_W_Cmd(SigMode,channel,0xBF);	
			   SSD2828_W_Data(SigMode,channel,0xB8); //GAMMA BLUE GROUP1
		     for( i = 0;i<15;i++)   //15个绑点
				 {	                                                                                  
					 SSD2828_W_Data(SigMode,channel,buffer[i+5]);	
					 delay_ms(5);  
				 }
				 SSD2828_W_Reg(SigMode,channel,0xBC,16); 
		     SSD2828_W_Cmd(SigMode,channel,0xBF);	
			   SSD2828_W_Data(SigMode,channel,0xB9); //GAMMA BLUE GROUP2
		     for( i = 0;i<15;i++)   //15个绑点
				 {	                                                                                  
					 SSD2828_W_Data(SigMode,channel,buffer[i+5+15]);	
					 delay_ms(5);  
				 }
				 SSD2828_W_Reg(SigMode,channel,0xBC,14); 
		     SSD2828_W_Cmd(SigMode,channel,0xBF);	
			   SSD2828_W_Data(SigMode,channel,0xBA); //GAMMA BLUE GROUP3
				  for( i = 0;i<13;i++)   //15个绑点
				 {	                                                                                  
					 SSD2828_W_Data(SigMode,channel,buffer[i+5+30]);	
					 delay_ms(5);  
				 }
		   
		} 

    buffer[4] = Uart_Error_None;
    buffer[5] = 0;
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);            //返回 写寄存器状态  ：ok  
	  SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}

void CH13726_Read_Gamma(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[7];
    u16 tmp;
    u8 i,j,k=0;
	  buffer1[0] = 0x02;        
    buffer1[1] = 0xf0;   
		buffer1[2] = 0x52;  
    SSD2828_W_Array(SigMode,channel,buffer1,0);
	
		SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
	  SSD2828_W_Reg(SigMode,channel,0xC1,0x000F); //返回个数设置
		SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
		delay_ms(10);
				 
		 if(buffer[3]==0x01)       //GAMMA RED
	  {
			   buffer1[0] = 0x02;        
         buffer1[1] = 0xB2;          
         SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	       delay_ms(5);                 //GAMMA RED GROUP1
			
				 SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
		     SSD2828_W_Cmd(SigMode,channel,0xFF);
		     delay_ms(5);
					for(i=0;i<15;i++)
					{
							SSD2828_W_Cmd(SigMode,channel,0xFA);		
							tmp=SPI3_Read_u16_Data(channel);
							buffer[4+i]=tmp>>8;
							buffer[5+i]=tmp;
							delay_ms(5);
							i++;			
					}
		   
	       SSD2828_W_Reg(SigMode,channel,0xC1,0x000F); //返回个数设置
		     SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
		     delay_ms(10);
				 
			   buffer1[0] = 0x02;        
         buffer1[1] = 0xB3;          
         SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	       delay_ms(5);  
				 SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
		     SSD2828_W_Cmd(SigMode,channel,0xFF);
		     delay_ms(5);
					for(i=0;i<15;i++)
					{
							SSD2828_W_Cmd(SigMode,channel,0xFA);		
							tmp=SPI3_Read_u16_Data(channel);
							buffer[4+15+i]=tmp>>8;
							buffer[5+15+i]=tmp;
							delay_ms(5);
							i++;			
					}
		
	       SSD2828_W_Reg(SigMode,channel,0xC1,0x000D); //返回个数设置
		     SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
		     delay_ms(10);
				 
			   buffer1[0] = 0x02;        
         buffer1[1] = 0xB4;          
         SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	       delay_ms(5);  
				 SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
				 SSD2828_W_Cmd(SigMode,channel,0xFF);
		     delay_ms(5);
					for(i=0;i<15;i++)
					{
							SSD2828_W_Cmd(SigMode,channel,0xFA);		
							tmp=SPI3_Read_u16_Data(channel);
							buffer[4+30+i]=tmp>>8;
							buffer[5+30+i]=tmp;
							delay_ms(5);
							i++;			
					}
	  }
		else if(buffer[3]==0x02)  //GAMMA GREEN
		{
			   buffer1[0] = 0x02;        
         buffer1[1] = 0xB5;          
         SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	       delay_ms(5);                 //GAMMA RED GROUP1
		   
				 SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
		     SSD2828_W_Cmd(SigMode,channel,0xFF);
		     delay_ms(5);
					for(i=0;i<15;i++)
					{
							SSD2828_W_Cmd(SigMode,channel,0xFA);		
							tmp=SPI3_Read_u16_Data(channel);
							buffer[4+i]=tmp>>8;
							buffer[5+i]=tmp;
							delay_ms(5);
							i++;			
					}
				 
				 SSD2828_W_Reg(SigMode,channel,0xC1,0x000F); //返回个数设置
		     SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
		     delay_ms(10);
					
			   buffer1[0] = 0x02;        
         buffer1[1] = 0xB6;          
         SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	       delay_ms(5);                 //GAMMA RED GROUP1
		
				 SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
		     SSD2828_W_Cmd(SigMode,channel,0xFF);
		     delay_ms(5);
					for(i=0;i<15;i++)
					{
							SSD2828_W_Cmd(SigMode,channel,0xFA);		
							tmp=SPI3_Read_u16_Data(channel);
							buffer[4+15+i]=tmp>>8;
							buffer[5+15+i]=tmp;
							delay_ms(5);
							i++;			
					}

				 SSD2828_W_Reg(SigMode,channel,0xC1,0x000F); //返回个数设置
		     SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
		     delay_ms(10);
					
			   buffer1[0] = 0x02;        
         buffer1[1] = 0xB7;          
         SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	       delay_ms(5);  
					
		   	 SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
				 SSD2828_W_Cmd(SigMode,channel,0xFF);
		     delay_ms(5);
					for(i=0;i<15;i++)
					{
							SSD2828_W_Cmd(SigMode,channel,0xFA);		
							tmp=SPI3_Read_u16_Data(channel);
							buffer[4+30+i]=tmp>>8;
							buffer[5+30+i]=tmp;
							delay_ms(5);
							i++;			
					}
		}
		else if(buffer[3]==0x03)   //GAMMA BLUE
		{
			   buffer1[0] = 0x02;        
         buffer1[1] = 0xB8;          
         SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	       delay_ms(5);                 //GAMMA RED GROUP1 
				 
				 SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
		     SSD2828_W_Cmd(SigMode,channel,0xFF);
		     delay_ms(5);
					for(i=0;i<15;i++)
					{
							SSD2828_W_Cmd(SigMode,channel,0xFA);		
							tmp=SPI3_Read_u16_Data(channel);
							buffer[4+i]=tmp>>8;
							buffer[5+i]=tmp;
							delay_ms(5);
							i++;			
					}
					
				 SSD2828_W_Reg(SigMode,channel,0xC1,0x000F); //返回个数设置
		     SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
		     delay_ms(10);
					
			   buffer1[0] = 0x02;        
         buffer1[1] = 0xB9;          
         SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	       delay_ms(5);                 //GAMMA RED GROUP1
				 
				 SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
		     SSD2828_W_Cmd(SigMode,channel,0xFF);
		     delay_ms(5);
					for(i=0;i<15;i++)
					{
							SSD2828_W_Cmd(SigMode,channel,0xFA);		
							tmp=SPI3_Read_u16_Data(channel);
							buffer[4+15+i]=tmp>>8;
							buffer[5+15+i]=tmp;
							delay_ms(5);
							i++;			
					}
					
				 SSD2828_W_Reg(SigMode,channel,0xC1,0x000F); //返回个数设置
		     SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
		     delay_ms(10);
			   buffer1[0] = 0x02;        
         buffer1[1] = 0xBA;          
         SSD2828_W_Array(SigMode,channel,buffer1,0);		 
	       delay_ms(5);  
				 SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
				 SSD2828_W_Cmd(SigMode,channel,0xFF);
		     delay_ms(5);
					for(i=0;i<15;i++)
					{
							SSD2828_W_Cmd(SigMode,channel,0xFA);		
							tmp=SPI3_Read_u16_Data(channel);
							buffer[4+30+i]=tmp>>8;
							buffer[5+30+i]=tmp;
							delay_ms(5);
							i++;			
					}
				}

				buffer[0]=0x71;	//CH13726
				buffer[1]=0x09;
				delay_ms(15);
				STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);          //返回 读取的 Gamma数据  ：ok  		
				SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
}

void CH13726_Gamma_OTP_Start(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[10];
	  u8 mtp_flag;
	  SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data);
		delay_ms(15); 
	  buffer1[0] = 0x01;
    buffer1[1] = 0x28;                   
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(40); 
		buffer1[0] = 0x01;
    buffer1[1] = 0x10;                   
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(120); 
		buffer1[0] = 0x02;
    buffer1[1] = 0x11;         
		buffer1[2] = 0x00;  	
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(120); 
		buffer1[0] = 0x02;
    buffer1[1] = 0xf0;         
		buffer1[2] = 0x51;  	
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
	
    buffer1[0] = 0x02;
    buffer1[1] = 0xEB;
    buffer1[2] = 0x11;                             
    SSD2828_W_Array(SigMode,channel,buffer1,0); 
		delay_ms(120); 
	
     // OTP ALL
		 buffer1[0] = 0x05;
     buffer1[1] = 0xED;
     buffer1[2] = 0x7F;  
		 buffer1[3] = 0xB0;
     buffer1[4] = 0x44;  
		 buffer1[5] = 0x30; 
		 SSD2828_W_Array(SigMode,channel,buffer1,0); 
	
	 
    buffer1[0] = 0x04;
    buffer1[1] = 0xEE;
    buffer1[2] = 0xA5;    //OTP_MODE_ACTIVE
	  buffer1[3] = 0x5A;    //OTP_MODE_ACTIVE
	  buffer1[4] = 0x3C;    //OTP_MODE_ACTIVE
		SSD2828_W_Array(SigMode,channel,buffer1,0);	
		delay_ms(600); 	
		
		 buffer1[0] = 0x05;
     buffer1[1] = 0xED;
     buffer1[2] = 0x00;  
		 buffer1[3] = 0x00;
     buffer1[4] = 0x00;  
		 buffer1[5] = 0x00; 
		 SSD2828_W_Array(SigMode,channel,buffer1,0); 
	
     buffer1[0] = 0x02;
     buffer1[1] = 0xEB;
     buffer1[2] = 0x00;  //
		 SSD2828_W_Array(SigMode,channel,buffer1,0); 
		 delay_ms(10);     	
		
		
		 buffer[3] = 0x00;   //返回 OTP Gamma数据  ：OK
		 buffer[4] = 0;
		STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);         
}


void CH13726_Read_OTP_Times(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u16 tmp;
    buffer[1]=0x03;
    SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);                               
    SSD2828_W_Reg(SigMode,channel,0xC1,0x0001); //return package size                                
    SSD2828_W_Reg(SigMode,channel,0xC0,0x0001);
    buffer[0] = 0x01;
    buffer[1] = 0xD8;                        
    SSD2828_W_Array(SigMode,channel,buffer,0);
    delay_ms(5);  
		SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
    SSD2828_W_Cmd(SigMode,channel,0xFF);

		SSD2828_W_Cmd(SigMode,channel,0xFA);		
		buffer[4]=SPI3_Read_u8_Data(channel);
		delay_ms(5);

		buffer[0]=0x2D;	
		buffer[1]=0x0B;
    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	
  	buffer[0]=0x71;
		buffer[3]=0x00;
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 寄存器数据  ：ok 
}
void CH13726_Gamma_Adjust_Over(USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[7];
    u8 i,j,m,k=0,temp;
	//退出gamma调节模式
	  SSD2828_W_Reg(SigMode,channel,0xB7,0x0019);
		buffer1[0] = 0x02;        
    buffer1[1] = 0xf0;   
		buffer1[2] = 0x58;  
    SSD2828_W_Array(SigMode,channel,buffer1,0);
	
		buffer1[0] = 0x04;        
    buffer1[1] = 0xc0;   
		buffer1[2] = 0x83;  
	  buffer1[3] = 0x6b; 
  	buffer1[4] = 0x94; 
    SSD2828_W_Array(SigMode,channel,buffer1,0);
		delay_ms(5);

    SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
	
    STM2PC(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 寄存器数据  ：ok 
}


void ProcessForIc71( USBD_HandleTypeDef *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	//void *pdev;
	switch(buffer[1]&0x0F)
	{
			case 0x01:                                      //写寄存器
				CH13726_Write_Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;	
			case 0x02:                                      //读寄存器
				CH13726_Read_Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;	
			case 0x03:                                      //写寄存器
				CH13726_Write_51Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;	
			case 0x04:                                      //写寄存器
				CH13726_AutoGamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;	
			case 0x08:                                      //写Gamma数据    
				CH13726_Write_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;
			case 0x09:                                      //读Gamma数据
				CH13726_Read_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break; 
			case 0x0A://gamma otp
			case 0x0D://OTP HBM
			case 0x0E://gamma adjust over
					CH13726_Gamma_Adjust_Over(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);  break;
			case 0x0F://OTP ID 	
				CH13726_Gamma_OTP_Start(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);  break;
			case 0x0B://OTP times
				CH13726_Read_OTP_Times(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);  break;							
			default:	break;	
	}			
}


